package com.banmao.rabbitmq.c3_topic;

import com.banmao.rabbitmq.utils.RabbitConstant;
import com.banmao.rabbitmq.utils.RabbitUtil;
import com.rabbitmq.client.*;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class Baidu {

    public static void main(String[] args) throws Exception {

        Connection connection = RabbitUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(RabbitConstant.WEATHER_BAIDU, false, false, false, null);

        // 绑定主题，模糊匹配为 china.guangdong.guangzhou. 开头的任何 RoutingKey
        channel.queueBind(RabbitConstant.WEATHER_BAIDU, RabbitConstant.EXCHANGE_WATHER_TOPIC, "china.guangdong.guangzhou.#");
        // 匹配 RoutingKey 格式为 *.*.*.20991011 的消息
        channel.queueBind(RabbitConstant.WEATHER_BAIDU, RabbitConstant.EXCHANGE_WATHER_TOPIC, "*.*.*.20991011");

        channel.basicQos(1);

        channel.basicConsume(RabbitConstant.WEATHER_BAIDU, false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                log.info("百度收到气象信息: {}", new String(body));
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        });

    }

}
